package com.atom.swipebacklib;

import android.support.v4.view.ViewCompat;
import android.util.Log;
import android.view.View;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * ============================================================
 * project: MySwipBackLayout
 * package: com.atom.swipbacklib
 * fileDescribe:
 * user: admin
 * email: 1299854942@qq.com
 * createTime: 2017/7/27  11:27
 * modify:
 * version:: V1.0
 * ============================================================
 **/
class SlidingPanelLayoutImpl16 implements SlidingPaneLayout {
    private static final String TAG = SlidingPanelLayoutImpl16.class.getSimpleName();

    private Method mGetDisplayList;
    private Field mRecreateDisplayList;

    public SlidingPanelLayoutImpl16(){
        try {
            mGetDisplayList = View.class.getDeclaredMethod("getDisplayList", (Class[]) null);
        } catch (NoSuchMethodException e) {
            Log.e(TAG, "Couldn't fetch getDisplayList method; dimming won't work right.", e);
        }
        try {
            mRecreateDisplayList = View.class.getDeclaredField("mRecreateDisplayList");
            mRecreateDisplayList.setAccessible(true);
        } catch (NoSuchFieldException e) {
            Log.e(TAG, "Couldn't fetch mRecreateDisplayList field; dimming will be slow.", e);
        }
    }

    @Override
    public void invalidateChildRegion(SwipeBackLayout parent, View child) {
        if (mGetDisplayList != null && mRecreateDisplayList != null) {
            try {
                mRecreateDisplayList.setBoolean(child, true);
                mGetDisplayList.invoke(child, (Object[]) null);
            } catch (Exception e) {
                Log.e(TAG, "Error refreshing display list state", e);
            }
        } else {
            // Slow path. REALLY slow path. Let's hope we don't get here.
            child.invalidate();
            return;
        }
        ViewCompat.postInvalidateOnAnimation(parent, child.getLeft(),child.getTop(),child.getRight(),child.getBottom());
    }
}
